#include <iostream>

using namespace std;

void Print(int* arr, int size)
{
	for (int i = 0; i< size; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}

void BubbleSort(int* arr, int size)
{
	for (int i = 0; i < size - 1; i++)
	{
		int exchange = 0;
		for (int j = 0; j < size - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				swap(arr[j], arr[j + 1]);
				exchange = 1;
			}
		}
		if (!exchange)
			break;
	}
}

void InsertSort(int* arr, int size)
{
	int end = 0;
	for (int i = 0; i < size - 1; i++)
	{
		end = i;
		int tmp = arr[end+1];
		while (end >= 0)
		{
			if (arr[end] > tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else {
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}

void ShellSort(int* arr, int size)
{
	int gap = size;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < size - gap; i++)
		{
			int end = i;
			int tmp = arr[i + gap];
			while (end >= 0)
			{
				if (arr[end] > tmp)
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else {
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}

int main()
{
	int arr[10] = { 1,5,6,8,4,3,0,9,7,2 };
	Print(arr, 10);

	/*BubbleSort(arr, 10);
	Print(arr, 10);*/

	/*InsertSort(arr, 10);
	Print(arr, 10);*/

	ShellSort(arr, 10);
	Print(arr, 10);

	return 0;
}